/*
 * TimeControl.java
 *
 * Created on 02 January 2004, 07:13
 * Copyright (C) 2004, Velocityme Partnership
 */

package com.velocityme.client.gui.time;

import com.velocityme.client.gui.exception.ExceptionWindow;
import com.velocityme.client.utility.StatelessRemoteClientSessionUtil;
import com.velocityme.interfaces.KeySession;
import com.velocityme.interfaces.NodePK;
import com.velocityme.interfaces.NodeUtil;
import com.velocityme.interfaces.TaskPK;
import com.velocityme.interfaces.TimePK;
import com.velocityme.utility.InvalidKeyException;
import com.velocityme.utility.InvalidTimeException;
import com.velocityme.utility.PermissionDeniedException;
import com.velocityme.valueobjects.StateValue;
import com.velocityme.valueobjects.TaskValue;
import com.velocityme.valueobjects.TimeValue;
import java.rmi.RemoteException;
import java.text.ParseException;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.naming.NamingException;
import javax.swing.JOptionPane;

/**
 *
 * @author  Robert Crida Work
 */
public class TimeControl extends javax.swing.JPanel {
    
    private KeySession m_key;
    private TaskPK m_taskPK;
    private TimeValueDisplay m_timeValueDisplay;

    /** Creates new form TimeControl */
    public TimeControl(KeySession p_key, TaskPK p_taskPK, TimePK p_timePK) {
        initComponents();
        m_key = p_key;
        m_taskPK = p_taskPK;
        try {
            String pathName = StatelessRemoteClientSessionUtil.getSession().getNodePathName(m_key, new NodePK(m_taskPK.getTaskId()));
            TaskValue taskValue = StatelessRemoteClientSessionUtil.getSession().getTaskValue(m_key, m_taskPK);
            StateValue stateValue = taskValue.getStateValue();
            StateValue[] allStateValues = StatelessRemoteClientSessionUtil.getSession().getTaskStateValues(m_key, m_taskPK);
            TimeValue timeValue;
            if (p_timePK != null)
                timeValue = StatelessRemoteClientSessionUtil.getSession().getTimeValue(m_key, p_timePK);
            else
                timeValue = new TimeValue();
            m_timeValueDisplay = new TimeValueDisplay(pathName, timeValue, taskValue.getPercentageComplete(), stateValue, allStateValues);
        }
        catch(Exception e) {
            ExceptionWindow.showError(e);
        }
        add(m_timeValueDisplay, java.awt.BorderLayout.CENTER);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        jPanel2 = new javax.swing.JPanel();
        jButtonOK = new javax.swing.JButton();
        jButtonCancel = new javax.swing.JButton();

        setLayout(new java.awt.BorderLayout());

        jPanel2.setLayout(new java.awt.GridLayout(1, 0));

        jButtonOK.setText("OK");
        jButtonOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonOKActionPerformed(evt);
            }
        });

        jPanel2.add(jButtonOK);

        jButtonCancel.setText("Cancel");
        jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelActionPerformed(evt);
            }
        });

        jPanel2.add(jButtonCancel);

        add(jPanel2, java.awt.BorderLayout.SOUTH);

    }//GEN-END:initComponents

    private void jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelActionPerformed
        // Add your handling code here:
        getParent().remove(this);                
    }//GEN-LAST:event_jButtonCancelActionPerformed

    private void jButtonOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOKActionPerformed
        // Add your handling code here:
        try {
            Byte percentageComplete = m_timeValueDisplay.getPercentageComplete();
            TimeValue timeValue = m_timeValueDisplay.getTimeValue();
            if (timeValue.timeIdHasBeenSet())
                StatelessRemoteClientSessionUtil.getSession().taskTimeEdit(m_key, percentageComplete, timeValue);
            else
                StatelessRemoteClientSessionUtil.getSession().taskTimeAssign(m_key, m_taskPK, percentageComplete, timeValue);
            getParent().remove(this);
        }
        catch(InvalidTimeException e) {
            JOptionPane.showMessageDialog(this, "Start time must be before stop time.", "Commit failed", JOptionPane.WARNING_MESSAGE);
        }
        catch(RemoteException e) {
            ExceptionWindow.showError(e);
        }
        catch(NamingException e) {
            ExceptionWindow.showError(e);
        }
        catch(CreateException e) {
            ExceptionWindow.showError(e);
        }
        catch(ParseException e) {
            ExceptionWindow.showError(e);
        }
        catch(PermissionDeniedException e) {
            ExceptionWindow.showError(e);
        }
        catch(InvalidKeyException e) {
            ExceptionWindow.showError(e);
        }
    }//GEN-LAST:event_jButtonOKActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCancel;
    private javax.swing.JButton jButtonOK;
    private javax.swing.JPanel jPanel2;
    // End of variables declaration//GEN-END:variables
    
}
